package xyz.scootaloo.oj.leetcode

import org.junit.Test
import xyz.scootaloo.common.TestDsl

/**
 * @author flutterdash@qq.com
 * @since 2021/7/2 20:59
 */
class P704 : TestDsl {

    private fun search(nums: IntArray, target: Int): Int {
        var left = 0
        var right = nums.size - 1
        var mid: Int
        var midValue: Int

        while (left <= right) {
            mid = (left + right).shr(1)
            midValue = nums[mid]
            when {
                midValue == target -> {
                    return mid
                }
                midValue > target -> {
                    right = mid - 1
                }
                else -> {
                    left = mid + 1
                }
            }
        }

        return -1
    }

    @Test
    fun test0() {
        val input = intArrayOf(-1, 0, 3, 5, 9, 12)
        search(input, 9) shouldBe 4
        search(input, 2) shouldBe -1
    }

}
